function make_8b10b_table()
%MAKE_8B10B_TABLE Print table definition for enc_8b10b_table.vhd
% See also: IEEE 802.3 Standard, Section 36, Table 36-1 and 36-2

% Copyright 2019 The Aerospace Corporation
%
% This file is part of SatCat5.
%
% SatCat5 is free software: you can redistribute it and/or modify it under
% the terms of the GNU Lesser General Public License as published by the
% Free Software Foundation, either version 3 of the License, or (at your
% option) any later version.
%
% SatCat5 is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
% FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
% License for more details.
%
% You should have received a copy of the GNU Lesser General Public License
% along with SatCat5.  If not, see <https://www.gnu.org/licenses/>.

% Definitions copied straight from IEEE standard:
% NOTE BITS ARE OUT OF ORDER!
%    RD-         RD+
%    abcdei fghj abcdei fghj
DATA_TABLE = { ...
    '100111 0100 011000 1011', ...
    '011101 0100 100010 1011', ...
    '101101 0100 010010 1011', ...
    '110001 1011 110001 0100', ...
    '110101 0100 001010 1011', ...
    '101001 1011 101001 0100', ...
    '011001 1011 011001 0100', ...
    '111000 1011 000111 0100', ...
    '111001 0100 000110 1011', ...
    '100101 1011 100101 0100', ...
    '010101 1011 010101 0100', ...
    '110100 1011 110100 0100', ...
    '001101 1011 001101 0100', ...
    '101100 1011 101100 0100', ...
    '011100 1011 011100 0100', ...
    '010111 0100 101000 1011', ...
    '011011 0100 100100 1011', ...
    '100011 1011 100011 0100', ...
    '010011 1011 010011 0100', ...
    '110010 1011 110010 0100', ...
    '001011 1011 001011 0100', ...
    '101010 1011 101010 0100', ...
    '011010 1011 011010 0100', ...
    '111010 0100 000101 1011', ...
    '110011 0100 001100 1011', ...
    '100110 1011 100110 0100', ...
    '010110 1011 010110 0100', ...
    '110110 0100 001001 1011', ...
    '001110 1011 001110 0100', ...
    '101110 0100 010001 1011', ...
    '011110 0100 100001 1011', ...
    '101011 0100 010100 1011', ...
    '100111 1001 011000 1001', ...
    '011101 1001 100010 1001', ...
    '101101 1001 010010 1001', ...
    '110001 1001 110001 1001', ...
    '110101 1001 001010 1001', ...
    '101001 1001 101001 1001', ...
    '011001 1001 011001 1001', ...
    '111000 1001 000111 1001', ...
    '111001 1001 000110 1001', ...
    '100101 1001 100101 1001', ...
    '010101 1001 010101 1001', ...
    '110100 1001 110100 1001', ...
    '001101 1001 001101 1001', ...
    '101100 1001 101100 1001', ...
    '011100 1001 011100 1001', ...
    '010111 1001 101000 1001', ...
    '011011 1001 100100 1001', ...
    '100011 1001 100011 1001', ...
    '010011 1001 010011 1001', ...
    '110010 1001 110010 1001', ...
    '001011 1001 001011 1001', ...
    '101010 1001 101010 1001', ...
    '011010 1001 011010 1001', ...
    '111010 1001 000101 1001', ...
    '110011 1001 001100 1001', ...
    '100110 1001 100110 1001', ...
    '010110 1001 010110 1001', ...
    '110110 1001 001001 1001', ...
    '001110 1001 001110 1001', ...
    '101110 1001 010001 1001', ...
    '011110 1001 100001 1001', ...
    '101011 1001 010100 1001', ...
    '100111 0101 011000 0101', ...
    '011101 0101 100010 0101', ...
    '101101 0101 010010 0101', ...
    '110001 0101 110001 0101', ...
    '110101 0101 001010 0101', ...
    '101001 0101 101001 0101', ...
    '011001 0101 011001 0101', ...
    '111000 0101 000111 0101', ...
    '111001 0101 000110 0101', ...
    '100101 0101 100101 0101', ...
    '010101 0101 010101 0101', ...
    '110100 0101 110100 0101', ...
    '001101 0101 001101 0101', ...
    '101100 0101 101100 0101', ...
    '011100 0101 011100 0101', ...
    '010111 0101 101000 0101', ...
    '011011 0101 100100 0101', ...
    '100011 0101 100011 0101', ...
    '010011 0101 010011 0101', ...
    '110010 0101 110010 0101', ...
    '001011 0101 001011 0101', ...
    '101010 0101 101010 0101', ...
    '011010 0101 011010 0101', ...
    '111010 0101 000101 0101', ...
    '110011 0101 001100 0101', ...
    '100110 0101 100110 0101', ...
    '010110 0101 010110 0101', ...
    '110110 0101 001001 0101', ...
    '001110 0101 001110 0101', ...
    '101110 0101 010001 0101', ...
    '011110 0101 100001 0101', ...
    '101011 0101 010100 0101', ...
    '100111 0011 011000 1100', ...
    '011101 0011 100010 1100', ...
    '101101 0011 010010 1100', ...
    '110001 1100 110001 0011', ...
    '110101 0011 001010 1100', ...
    '101001 1100 101001 0011', ...
    '011001 1100 011001 0011', ...
    '111000 1100 000111 0011', ...
    '111001 0011 000110 1100', ...
    '100101 1100 100101 0011', ...
    '010101 1100 010101 0011', ...
    '110100 1100 110100 0011', ...
    '001101 1100 001101 0011', ...
    '101100 1100 101100 0011', ...
    '011100 1100 011100 0011', ...
    '010111 0011 101000 1100', ...
    '011011 0011 100100 1100', ...
    '100011 1100 100011 0011', ...
    '010011 1100 010011 0011', ...
    '110010 1100 110010 0011', ...
    '001011 1100 001011 0011', ...
    '101010 1100 101010 0011', ...
    '011010 1100 011010 0011', ...
    '111010 0011 000101 1100', ...
    '110011 0011 001100 1100', ...
    '100110 1100 100110 0011', ...
    '010110 1100 010110 0011', ...
    '110110 0011 001001 1100', ...
    '001110 1100 001110 0011', ...
    '101110 0011 010001 1100', ...
    '011110 0011 100001 1100', ...
    '101011 0011 010100 1100', ...
    '100111 0010 011000 1101', ...
    '011101 0010 100010 1101', ...
    '101101 0010 010010 1101', ...
    '110001 1101 110001 0010', ...
    '110101 0010 001010 1101', ...
    '101001 1101 101001 0010', ...
    '011001 1101 011001 0010', ...
    '111000 1101 000111 0010', ...
    '111001 0010 000110 1101', ...
    '100101 1101 100101 0010', ...
    '010101 1101 010101 0010', ...
    '110100 1101 110100 0010', ...
    '001101 1101 001101 0010', ...
    '101100 1101 101100 0010', ...
    '011100 1101 011100 0010', ...
    '010111 0010 101000 1101', ...
    '011011 0010 100100 1101', ...
    '100011 1101 100011 0010', ...
    '010011 1101 010011 0010', ...
    '110010 1101 110010 0010', ...
    '001011 1101 001011 0010', ...
    '101010 1101 101010 0010', ...
    '011010 1101 011010 0010', ...
    '111010 0010 000101 1101', ...
    '110011 0010 001100 1101', ...
    '100110 1101 100110 0010', ...
    '010110 1101 010110 0010', ...
    '110110 0010 001001 1101', ...
    '001110 1101 001110 0010', ...
    '101110 0010 010001 1101', ...
    '011110 0010 100001 1101', ...
    '101011 0010 010100 1101', ...
    '100111 1010 011000 1010', ...
    '011101 1010 100010 1010', ...
    '101101 1010 010010 1010', ...
    '110001 1010 110001 1010', ...
    '110101 1010 001010 1010', ...
    '101001 1010 101001 1010', ...
    '011001 1010 011001 1010', ...
    '111000 1010 000111 1010', ...
    '111001 1010 000110 1010', ...
    '100101 1010 100101 1010', ...
    '010101 1010 010101 1010', ...
    '110100 1010 110100 1010', ...
    '001101 1010 001101 1010', ...
    '101100 1010 101100 1010', ...
    '011100 1010 011100 1010', ...
    '010111 1010 101000 1010', ...
    '011011 1010 100100 1010', ...
    '100011 1010 100011 1010', ...
    '010011 1010 010011 1010', ...
    '110010 1010 110010 1010', ...
    '001011 1010 001011 1010', ...
    '101010 1010 101010 1010', ...
    '011010 1010 011010 1010', ...
    '111010 1010 000101 1010', ...
    '110011 1010 001100 1010', ...
    '100110 1010 100110 1010', ...
    '010110 1010 010110 1010', ...
    '110110 1010 001001 1010', ...
    '001110 1010 001110 1010', ...
    '101110 1010 010001 1010', ...
    '011110 1010 100001 1010', ...
    '101011 1010 010100 1010', ...
    '100111 0110 011000 0110', ...
    '011101 0110 100010 0110', ...
    '101101 0110 010010 0110', ...
    '110001 0110 110001 0110', ...
    '110101 0110 001010 0110', ...
    '101001 0110 101001 0110', ...
    '011001 0110 011001 0110', ...
    '111000 0110 000111 0110', ...
    '111001 0110 000110 0110', ...
    '100101 0110 100101 0110', ...
    '010101 0110 010101 0110', ...
    '110100 0110 110100 0110', ...
    '001101 0110 001101 0110', ...
    '101100 0110 101100 0110', ...
    '011100 0110 011100 0110', ...
    '010111 0110 101000 0110', ...
    '011011 0110 100100 0110', ...
    '100011 0110 100011 0110', ...
    '010011 0110 010011 0110', ...
    '110010 0110 110010 0110', ...
    '001011 0110 001011 0110', ...
    '101010 0110 101010 0110', ...
    '011010 0110 011010 0110', ...
    '111010 0110 000101 0110', ...
    '110011 0110 001100 0110', ...
    '100110 0110 100110 0110', ...
    '010110 0110 010110 0110', ...
    '110110 0110 001001 0110', ...
    '001110 0110 001110 0110', ...
    '101110 0110 010001 0110', ...
    '011110 0110 100001 0110', ...
    '101011 0110 010100 0110', ...
    '100111 0001 011000 1110', ...
    '011101 0001 100010 1110', ...
    '101101 0001 010010 1110', ...
    '110001 1110 110001 0001', ...
    '110101 0001 001010 1110', ...
    '101001 1110 101001 0001', ...
    '011001 1110 011001 0001', ...
    '111000 1110 000111 0001', ...
    '111001 0001 000110 1110', ...
    '100101 1110 100101 0001', ...
    '010101 1110 010101 0001', ...
    '110100 1110 110100 1000', ...
    '001101 1110 001101 0001', ...
    '101100 1110 101100 1000', ...
    '011100 1110 011100 1000', ...
    '010111 0001 101000 1110', ...
    '011011 0001 100100 1110', ...
    '100011 0111 100011 0001', ...
    '010011 0111 010011 0001', ...
    '110010 1110 110010 0001', ...
    '001011 0111 001011 0001', ...
    '101010 1110 101010 0001', ...
    '011010 1110 011010 0001', ...
    '111010 0001 000101 1110', ...
    '110011 0001 001100 1110', ...
    '100110 1110 100110 0001', ...
    '010110 1110 010110 0001', ...
    '110110 0001 001001 1110', ...
    '001110 1110 001110 0001', ...
    '101110 0001 010001 1110', ...
    '011110 0001 100001 1110', ...
    '101011 0001 010100 1110'};
%    RD-         RD+
%    abcdei fghj abcdei fghj
CTRL_TABLE = { ...
    '001111 0100 110000 1011', ...
    '001111 1001 110000 0110', ...
    '001111 0101 110000 1010', ...
    '001111 0011 110000 1100', ...
    '001111 0010 110000 1101', ...
    '001111 1010 110000 0101', ...
    '001111 0110 110000 1001', ...
    '001111 1000 110000 0111', ...
    '111010 1000 000101 0111', ...
    '110110 1000 001001 0111', ...
    '101110 1000 010001 0111', ...
    '011110 1000 100001 0111'};
%    abcdei fghj abcdei fghj
%    12345678901234567890123

% Count number of bits in a given string:
count_bits = @(str) sum(logical(str == '1'));
toggle_rd = @(str) sprintf('%d', logical(count_bits(str) ~= 5));

% Print a single line from the table:
% MSB is the RD toggle flag, then "abcdefghij" in order.
% Note order change from raw string, which is "abcdei fghj"!
print_line = @(str) fprintf(1, '    "%c%s%s%c%c",\n', ...
    toggle_rd(str), str(1:5), str(8:10), str(6), str(11));

% Get either the RD- or RD+ version from the string table:
get_str = @(row, rdp) row((1+12*rdp):(11+12*rdp));

% Print the data table: RD-, then RD+.
for rd = 0:1
    for n = 0:255
        str = get_str(DATA_TABLE{n+1}, rd);
        print_line(str);
    end
end

% The control table only has twelve valid symbols.
% For all others, return the "error token" /V/ = K.30.7.
for rd = 0:1
    for n = 0:255
        if     (n == hex2dec('1C')), idx = 1;   % K.28.0
        elseif (n == hex2dec('3C')), idx = 2;   % K.28.1
        elseif (n == hex2dec('5C')), idx = 3;   % K.28.2
        elseif (n == hex2dec('7C')), idx = 4;   % K.28.3
        elseif (n == hex2dec('9C')), idx = 5;   % K.28.4
        elseif (n == hex2dec('BC')), idx = 6;   % K.28.5
        elseif (n == hex2dec('DC')), idx = 7;   % K.28.6
        elseif (n == hex2dec('FC')), idx = 8;   % K.28.7
        elseif (n == hex2dec('F7')), idx = 9;   % K.23.7
        elseif (n == hex2dec('FB')), idx = 10;  % K.27.7
        elseif (n == hex2dec('FD')), idx = 11;  % K.29.7
        else                         idx = 12;  % K.30.7
        end
        str = get_str(CTRL_TABLE{idx}, rd);
        print_line(str);
    end
end

end
